﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
using AjaxControlToolkit;
using Newtonsoft.Json;

using CE.HotelEan;
using HotelWS;
using CE.Expedia;


public partial class travel_hotels_hotels_search : System.Web.UI.Page
{
    private string _apikey = "98pndgasdzzxpugjzqpxw6za";
    private int _minorrev = 13;
    private Int64 _cid = 381988;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["hotelsearchid"] = null;            

            DateTime _TravelDate = CE.Global.General.GetUTCDateTime().AddDays(+3);
            DateTime _TravelOutDate = CE.Global.General.GetUTCDateTime().AddDays(+5);

            inputCheckinDate.Value = _TravelDate.ToString("dd/MM/yyyy");
            inputCheckoutDate.Value = _TravelOutDate.ToString("dd/MM/yyyy");

            FillChildAge();
            //GetLocationTest();

            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());
            string domainUrl = "http://" + myUri.Host;
            hlnkManage.NavigateUrl = domainUrl + "/travel-hotels/booking/manage.aspx";

            hlnkManage.NavigateUrl = hlnkManage.ResolveClientUrl(hlnkManage.NavigateUrl).Replace("http", "https");
        }

    }

    //private void GetLocationTest()
    //{
    //    HotelServicesImplService hws = new HotelServicesImplService();

    //    LocationInfoRequest locinforeq = new LocationInfoRequest();
    //    locinforeq.apiKey = _apikey;
    //    locinforeq.cid = _cid;
    //    locinforeq.minorRev = _minorrev;
    //    locinforeq.customerIpAddress = Request.UserHostAddress;
    //    locinforeq.customerUserAgent = Request.Browser.ToString();
    //    locinforeq.customerSessionId = "";
    //    locinforeq.destinationString = "angkor wat";
    //    locinforeq.type = LocationInfoType.Item2;
    //    locinforeq.localeSpecified = true;
    //    locinforeq.ignoreSearchWeight = true;
    //    //locinforeq.city = "manila";
    //    locinforeq.locale = LocaleType.en_US;
    //    locinforeq.typeSpecified = true;

    //    LocationInfoResponse locinforesp = new LocationInfoResponse();
    //    locinforesp = hws.getGeoLocation(locinforeq);

    //    string destid = "";

    //    if (locinforesp != null && locinforesp.EanWsError == null)
    //    {
    //        //lblTest.Text = locinforesp.LocationInfos.LocationInfo.;

    //        foreach (LocationInfo locinfo in locinforesp.LocationInfos.LocationInfo)
    //        {
    //            if (locinfo.stateProvinceCode != null && locinfo.stateProvinceCode != "")
    //            {
    //                lblTest.Text += locinfo.description + " -- " + locinfo.city + ", " + locinfo.stateProvinceCode + ", " + locinfo.countryName + ", " + locinfo.countryCode;
    //            }
    //            else
    //            {
    //                lblTest.Text += locinfo.description + " -- " + locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode + "<br/>";
    //            }
    //        }

    //        destid = locinforesp.LocationInfos.LocationInfo[0].destinationId;
    //    }
    //    else
    //    {
    //        lblTest.Text = locinforesp.EanWsError.presentationMessage;
    //    }

    //    LocationInfoRequest locinforeq2 = new LocationInfoRequest();
    //    locinforeq2.apiKey = _apikey;
    //    locinforeq2.cid = _cid;
    //    locinforeq2.minorRev = _minorrev;
    //    locinforeq2.customerIpAddress = Request.UserHostAddress;
    //    locinforeq2.customerUserAgent = Request.Browser.ToString();
    //    locinforeq2.customerSessionId = "";
    //    //locinforeq2.destinationId = destid; // "7E2ED1CF-2BE5-437F-95F4-4E2E2EA606F7";
    //    locinforeq2.destinationString = "manila";
    //    locinforeq2.type = LocationInfoType.Item2;
    //    locinforeq2.locale = LocaleType.en_US;
    //    locinforeq2.localeSpecified = true;
    //    locinforeq2.ignoreSearchWeight = true;
    //    locinforeq2.typeSpecified = true;

    //    LocationInfoResponse locinforesp2 = new LocationInfoResponse();
    //    locinforesp2 = hws.getGeoLocation(locinforeq2);

    //    string destid2 = "";
        
    //    if (locinforesp2 != null && locinforesp2.EanWsError == null)
    //    {
    //        //lblTest.Text = locinforesp.LocationInfos.LocationInfo.;

    //        //foreach (LocationInfo locinfo in locinforesp2.LocationInfos.LocationInfo)
    //        //{
    //        //    lblTest2.Text += locinfo.description + " -- " + locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode + "<br/>";
    //        //}

    //        destid2 = locinforesp2.LocationInfos.LocationInfo[0].destinationId;

    //        if (locinforesp2 != null)
    //        {
    //            DataTable dtLandmarks = new DataTable();
    //            DataColumn col1 = new DataColumn("destinationId");
    //            DataColumn col2 = new DataColumn("countryCode");
    //            DataColumn col3 = new DataColumn("countryName");
    //            DataColumn col4 = new DataColumn("description");
    //            DataColumn col5 = new DataColumn("latitude");
    //            DataColumn col6 = new DataColumn("longitude");

    //            dtLandmarks.Columns.Add(col1);
    //            dtLandmarks.Columns.Add(col2);
    //            dtLandmarks.Columns.Add(col3);
    //            dtLandmarks.Columns.Add(col4);
    //            dtLandmarks.Columns.Add(col5);
    //            dtLandmarks.Columns.Add(col6);

    //            foreach (LocationInfo locinfo in locinforesp2.LocationInfos.LocationInfo)
    //            {
    //                DataRow row = dtLandmarks.NewRow();
    //                row["destinationId"] = locinfo.destinationId;
    //                row["countryCode"] = locinfo.countryCode;
    //                row["countryName"] = locinfo.countryName;
    //                row["description"] = locinfo.description;
    //                row["latitude"] = locinfo.latitude;
    //                row["longitude"] = locinfo.longitude;

    //                dtLandmarks.Rows.Add(row);

    //                //locitemrow = locinfo.description + ", " + locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode;
    //                //locitemrow = locinfo.description + ", " + locinfo.city + ", " + locinfo.countryName;
    //                //locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

    //                //LocationNames.Add(locitem);
    //            }


    //            DataView dvLandmarks = new DataView(dtLandmarks);
    //            dvLandmarks.RowFilter = "description like 'manila%'";
    //            //dvLandmarks.Sort = "latitude, longitude asc";

    //            DataTable dtFiltered = new DataTable();
    //            dtFiltered = dvLandmarks.ToTable();

    //            // get top 10
    //            int i = 0;
    //            foreach (DataRow drow in dtFiltered.Rows)
    //            {
    //                string rowdesc = drow["description"].ToString() + ", " + drow["countryName"].ToString() + ", " + drow["countryCode"].ToString();
    //                //locitem = AutoCompleteExtender.CreateAutoCompleteItem(rowdesc, drow["destinationId"].ToString());

    //                //LocationNames.Add(locitem);

    //                lblTest2.Text += rowdesc;

    //                i++;

    //                if (i >= 10)
    //                {
    //                    break;
    //                }
    //            }


    //        }
    //    }
    //    else
    //    {
    //        lblTest2.Text = locinforesp2.EanWsError.presentationMessage;
    //    }





    //    //LocationInfoRequest locinforeq3 = new LocationInfoRequest();
    //    //locinforeq3.apiKey = "98pndgasdzzxpugjzqpxw6za";
    //    //locinforeq3.cid = 381988;
    //    //locinforeq3.minorRev = 13;
    //    //locinforeq3.customerIpAddress = Request.UserHostAddress;
    //    //locinforeq3.customerUserAgent = Request.Browser.ToString();
    //    //locinforeq3.customerSessionId = "";
    //    //locinforeq3.destinationId = destid2; // "0427ABE2-72C3-4716-983B-8A685869BA52"; // destid2;
    //    ////locinforeq3.destinationString = "dallas";
    //    ////locinforeq3.type = LocationInfoType.Item2;
    //    //locinforeq3.locale = LocaleType.en_US;
    //    //locinforeq3.localeSpecified = true;
    //    //locinforeq3.ignoreSearchWeight = true;

    //    //LocationInfoResponse locinforesp3 = new LocationInfoResponse();
    //    //locinforesp3 = hws.getGeoLocation(locinforeq3);

    //    //if (locinforesp3 != null && locinforesp3.EanWsError == null)
    //    //{
    //    //    //lblTest.Text = locinforesp.LocationInfos.LocationInfo.;

    //    //    foreach (LocationInfo locinfo in locinforesp3.LocationInfos.LocationInfo)
    //    //    {
    //    //        lblTest3.Text += locinfo.description + " -- " + locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode + "<br/>";
    //    //    }
    //    //}
    //    //else
    //    //{
    //    //    lblTest3.Text = locinforesp3.EanWsError.presentationMessage;
    //    //}


    //    //Session["hotelcustomerid"]

    //    //hws.get


    //}

    //[System.Web.Services.WebMethod]
    //public static string[] GetLocationSearch(string prefixText)
    //{
    //    string _apikey = "98pndgasdzzxpugjzqpxw6za";
    //    int _minorrev = 13;
    //    Int64 _cid = 381988;
     
    //    List<string> LocationNames = new List<string>();
     
    //    HotelServicesImplService hws = new HotelServicesImplService();

    //    LocationInfoRequest locinforeq = new LocationInfoRequest();
    //    locinforeq.apiKey = _apikey;
    //    locinforeq.cid = _cid;
    //    locinforeq.minorRev = _minorrev;
    //    locinforeq.minorRevSpecified = true;
    //    locinforeq.customerIpAddress = "";// Request.UserHostAddress;
    //    locinforeq.customerUserAgent = ""; // Request.Browser.ToString();
    //    locinforeq.customerSessionId = "";
    //    //locinforeq.city = prefixText;
    //    //locinforeq.stateProvinceCode = "";
    //    //locinforeq.countryCode = "";
    //    locinforeq.type = LocationInfoType.Item1;
    //    //locinforeq.type = LocationInfoType.Item2;
    //    //locinforeq.address = prefixText;
    //    locinforeq.destinationString = prefixText;
    //    locinforeq.locale = LocaleType.en_US;
    //    locinforeq.localeSpecified = true;
    //    locinforeq.ignoreSearchWeight = true;
    //    locinforeq.useGeocoder = false;

    //    LocationInfoResponse locinforesp = new LocationInfoResponse();
    //    locinforesp = hws.getGeoLocation(locinforeq);
    //    string destid = "";
    //    string locitem = "";
    //    string locitemrow = "";

    //    if (locinforesp.EanWsError == null)
    //    {
    //        foreach (LocationInfo locinfo in locinforesp.LocationInfos.LocationInfo)
    //        {
    //            if (locinfo.stateProvinceCode != null && locinfo.stateProvinceCode != "")
    //            {
    //                locitemrow = locinfo.city + ", " + locinfo.stateProvinceCode + ", " + locinfo.countryName + ", " + locinfo.countryCode;
    //                locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

    //                LocationNames.Add(locitem);

    //            }
    //            else
    //            {
    //                locitemrow = locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode;
    //                locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

    //                LocationNames.Add(locitem);

    //            }

    //            //destid = locinforesp.LocationInfos.LocationInfo[0].destinationId;

    //        }
    //    }
    //    else
    //    {
    //        locitem = "no result. try other keywords";

    //        LocationNames.Add(locitem);
    //    }

    //    //LocationInfoRequest locinforeqLandmark = new LocationInfoRequest();
    //    //locinforeqLandmark.apiKey = _apikey;
    //    //locinforeqLandmark.cid = _cid;
    //    //locinforeqLandmark.minorRev = _minorrev;
    //    //locinforeqLandmark.minorRevSpecified = true;
    //    //locinforeqLandmark.customerIpAddress = "";//Request.UserHostAddress;
    //    //locinforeqLandmark.customerUserAgent = "";// Request.Browser.ToString();
    //    //locinforeqLandmark.customerSessionId = "";
    //    ////locinforeqLandmark.destinationId = destid;
    //    //locinforeqLandmark.destinationString = prefixText;
    //    //locinforeqLandmark.type = LocationInfoType.Item2;
    //    //locinforeqLandmark.locale = LocaleType.en_US;
    //    //locinforeqLandmark.localeSpecified = true;
    //    //locinforeqLandmark.ignoreSearchWeight = true;
    //    //locinforeqLandmark.typeSpecified = true;

    //    //LocationInfoResponse locinforespLandmark = new LocationInfoResponse();
    //    //locinforespLandmark = hws.getGeoLocation(locinforeqLandmark);

    //    //if (locinforespLandmark != null)
    //    //{
    //    //    //DataTable dtLandmarks = new DataTable();
    //    //    //DataColumn col1 = new DataColumn("destinationId");
    //    //    //DataColumn col2 = new DataColumn("countryCode");
    //    //    //DataColumn col3 = new DataColumn("countryName");
    //    //    //DataColumn col4 = new DataColumn("description");
    //    //    //DataColumn col5 = new DataColumn("latitude");
    //    //    //DataColumn col6 = new DataColumn("longitude");

    //    //    //dtLandmarks.Columns.Add(col1);
    //    //    //dtLandmarks.Columns.Add(col2);
    //    //    //dtLandmarks.Columns.Add(col3);
    //    //    //dtLandmarks.Columns.Add(col4);
    //    //    //dtLandmarks.Columns.Add(col5);
    //    //    //dtLandmarks.Columns.Add(col6);

    //    //    int i = 1;
    //    //    foreach (LocationInfo locinfo in locinforespLandmark.LocationInfos.LocationInfo)
    //    //    {
    //    //        //DataRow row = dtLandmarks.NewRow();
    //    //        //row["destinationId"] = locinfo.destinationId;
    //    //        //row["countryCode"] = locinfo.countryCode;
    //    //        //row["countryName"] = locinfo.countryName;
    //    //        //row["description"] = locinfo.description;
    //    //        //row["latitude"] = locinfo.latitude;
    //    //        //row["longitude"] = locinfo.longitude;

    //    //        //dtLandmarks.Rows.Add(row);

    //    //        locitemrow = locinfo.description + ", " + locinfo.countryName + ", " + locinfo.countryCode;
    //    //        //locitemrow = locinfo.description + ", " + locinfo.countryName;
    //    //        locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

    //    //        LocationNames.Add(locitem);

    //    //        i++;

    //    //        if (i >= 10)
    //    //        {
    //    //            break;
    //    //        }
    //    //    }


    //    //    //DataView dvLandmarks = new DataView(dtLandmarks);
    //    //    //dvLandmarks.RowFilter = "description like '" + prefixText + "%'";
    //    //    ////dvLandmarks.Sort = "latitude asc";

    //    //    //DataTable dtFiltered = new DataTable();
    //    //    //dtFiltered = dvLandmarks.ToTable();

    //    //    //// get top 10
    //    //    //int i = 0;
    //    //    //foreach (DataRow drow in dtFiltered.Rows)
    //    //    //{
    //    //    //    string rowdesc = drow["description"].ToString() + ", " + drow["countryName"].ToString() + ", " + drow["countryCode"].ToString();
    //    //    //    locitem = AutoCompleteExtender.CreateAutoCompleteItem(rowdesc, drow["destinationId"].ToString());

    //    //    //    LocationNames.Add(locitem);

    //    //    //    i++;

    //    //    //    if (i >= 10)
    //    //    //    {
    //    //    //        break;
    //    //    //    }
    //    //    //}


    //    //}

    //    return LocationNames.ToArray();
    //}

    private string[] GetLocationByDestination(string destination, string searchtype)
    {
        string[] location = new string[2];
        HotelServicesImplService hws = new HotelServicesImplService();

        LocationInfoRequest locinforeq = new LocationInfoRequest();
        locinforeq.apiKey = _apikey;
        locinforeq.cid = _cid;
        locinforeq.minorRev = _minorrev;
        locinforeq.customerIpAddress = Request.UserHostAddress;
        locinforeq.customerUserAgent = Request.Browser.ToString();
        locinforeq.customerSessionId = "";
        locinforeq.destinationString = destination;
        locinforeq.localeSpecified = true;
        locinforeq.ignoreSearchWeight = true;

        if (searchtype == "city")
        {
            locinforeq.type = LocationInfoType.Item1;
        }
        else if (searchtype == "airport")
        {
            locinforeq.type = LocationInfoType.Item4;
        }

        string getData = "http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=" + locinforeq.cid
                + "&minorRev=26&apiKey=" + locinforeq.apiKey
                + "&customerIpAddress=" + locinforeq.customerIpAddress
                + "&customerUserAgent=" + locinforeq.customerUserAgent
                + "&_type=xml&customerSessionId=" + locinforeq.customerSessionId
                + "&xml="
                + "<LocationInfoRequest><type>" + locinforeq.type +
                "</type><destinationString>" + locinforeq.destinationString +
                "</destinationString><ignoreSearchWeight>" + locinforeq.ignoreSearchWeight +
                "</ignoreSearchWeight>";

        getData += "</LocationInfoRequest>";

        CXLocation locinforesp = new CXLocation();

        // Handle JsonConvert array bug
        string returnJson = "";
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.LoadXml(getXMLResult(getData));
        foreach (System.Xml.XmlNode item in doc.SelectNodes("//LocationInfos"))
        {
            item.AppendChild(doc.CreateNode("element", "LocationInfo", ""));
        }
        returnJson = JsonConvert.SerializeXmlNode(doc).Replace(",null]", "]");

        returnJson = returnJson.Replace("@", "");
        returnJson = returnJson.Replace("\"xmlns:ns2\":\"http://v3.hotel.wsapi.ean.com/\",", "");
        returnJson = returnJson.Replace("ns2:", "");
        locinforesp = JsonConvert.DeserializeObject<CXLocation>(returnJson);

        //LocationInfoResponse locinforesp = new LocationInfoResponse();
        //locinforesp = hws.getGeoLocation(locinforeq);

        if (locinforesp != null & locinforesp.LocationInfoResponse.EanWsError == null)
        {
            if (locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].stateProvinceCode != null && locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].stateProvinceCode != "")
            {
                location[0] = locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].city + ", " + locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].stateProvinceCode
                    + ", " + locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].countryName + ", " + locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].countryCode;
            }
            else
            {
                location[0] = locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].city + ", " + locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].countryName + ", " + locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].countryCode;
            }

            location[1] = locinforesp.LocationInfoResponse.LocationInfos.LocationInfo[0].destinationId;

        }
        else
        {
            location[0] = locinforesp.LocationInfoResponse.EanWsError.presentationMessage;
            location[1] = "";
        }

        return location;
    }

    [System.Web.Services.WebMethod]
    public static string[] GetLocationSearchNew(string prefixText)
    {
        //string _apikey = "98pndgasdzzxpugjzqpxw6za";
        //int _minorrev = 13;
        //Int64 _cid = 381988;

        List<string> LocationNames = new List<string>();

        //HotelServicesImplService hws = new HotelServicesImplService();

        //LocationInfoRequest locinforeq = new LocationInfoRequest();
        //locinforeq.apiKey = _apikey;
        //locinforeq.cid = _cid;
        //locinforeq.minorRev = _minorrev;
        //locinforeq.minorRevSpecified = true;
        //locinforeq.customerIpAddress = "";// Request.UserHostAddress;
        //locinforeq.customerUserAgent = ""; // Request.Browser.ToString();
        //locinforeq.customerSessionId = "";
        //locinforeq.type = LocationInfoType.Item1;
        //locinforeq.destinationString = prefixText;
        //locinforeq.locale = LocaleType.en_US;
        //locinforeq.localeSpecified = true;
        //locinforeq.ignoreSearchWeight = true;
        //locinforeq.useGeocoder = false;

        //LocationInfoResponse locinforesp = new LocationInfoResponse();
        //locinforesp = hws.getGeoLocation(locinforeq);

        string destid = "";
        string locitem = "";
        string locitemrow = "";

        //if (locinforesp.EanWsError == null)
        //{
        //    foreach (LocationInfo locinfo in locinforesp.LocationInfos.LocationInfo)
        //    {
        //        if (locinfo.stateProvinceCode != null && locinfo.stateProvinceCode != "")
        //        {
        //            locitemrow = locinfo.city + ", " + locinfo.stateProvinceCode + ", " + locinfo.countryName + ", " + locinfo.countryCode;
        //            locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

        //            LocationNames.Add(locitem);

        //        }
        //        else
        //        {
        //            locitemrow = locinfo.city + ", " + locinfo.countryName + ", " + locinfo.countryCode;
        //            locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, locinfo.destinationId);

        //            LocationNames.Add(locitem);

        //        }

        //    }
        //}
        //else
        //{
        //    locitem = "no result. try other keywords";

        //    LocationNames.Add(locitem);
        //}
                

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CX_DBConnectionString"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select top 9 DestinationId, Destination, Country, StateProvince, CountryCode from tblexpediadestinationdetail where CountryCode is not null and Destination like '" + prefixText + "%'", con);
        //cmd.Parameters.AddWithValue("@Name", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
      
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //CountryNames.Add(dt.Rows[i][1].ToString());

                //SqlCommand cmd2 = new SqlCommand("select top 1 DestinationID from tblexpediadestinationdetail "
                //                + "where CenterLatitude = (select top 1 CenterLatitude from tblExpediaRegionCenterCoordinatesList where RegionID = " + dt.Rows[i]["RegionID"].ToString() + ") "
                //                + "and CenterLongitude = (select top 1 CenterLongitude from tblExpediaRegionCenterCoordinatesList where RegionID = " + dt.Rows[i]["RegionID"].ToString() + ") ", con);

                //SqlCommand cmd2 = new SqlCommand("select top 1 replace(replace(destinationid, '{', ''), '}', '') as DestinationID from tblExpediaLandmark "
                //                + "where CenterLatitude = (select top 1 CenterLatitude from tblExpediaRegionCenterCoordinatesList where RegionID = " + dt.Rows[i]["RegionID"].ToString() + ") "
                //                + "and CenterLongitude = (select top 1 CenterLongitude from tblExpediaRegionCenterCoordinatesList where RegionID = " + dt.Rows[i]["RegionID"].ToString() + ") ", con);

                //SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                //DataTable dtDest = new DataTable();
                //da2.Fill(dtDest);

                //if (dtDest.Rows.Count > 0)
                //{
                //    destid = dtDest.Rows[0]["DestinationID"].ToString();
                // }

                //locitemrow = dt.Rows[i]["RegionName"].ToString();
                destid = dt.Rows[i]["DestinationID"].ToString();

                if (dt.Rows[i]["StateProvince"].ToString() != "")
                    locitemrow = dt.Rows[i]["Destination"].ToString() + ", " + dt.Rows[i]["StateProvince"].ToString() + ", " + dt.Rows[i]["Country"].ToString() + ", " + dt.Rows[i]["CountryCode"].ToString();
                else
                    locitemrow = dt.Rows[i]["Destination"].ToString() + ", " + dt.Rows[i]["Country"].ToString() + ", " + dt.Rows[i]["CountryCode"].ToString();

                locitem = AutoCompleteExtender.CreateAutoCompleteItem(locitemrow, destid);

                LocationNames.Add(locitem);
            }
        }
        else
        {
            LocationNames.Add("no result. try other keywords ");
        }

     
        return LocationNames.ToArray();
    }

    private void FillChildAge()
    {
        for (int i = 17; i >= 0; i--)
        {
            if (i == 0)
            {
                ddlChildRoom1Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom1Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom1Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom1Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom2Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom2Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom2Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom2Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom3Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom3Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom3Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom3Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom4Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom4Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom4Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom4Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom5Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom5Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom5Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom5Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom6Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom6Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom6Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom6Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom7Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom7Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom7Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom7Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));

                ddlChildRoom8Child1Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom8Child2Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom8Child3Age.Items.Insert(0, new ListItem("<1", i.ToString()));
                ddlChildRoom8Child4Age.Items.Insert(0, new ListItem("<1", i.ToString()));
            }
            else
            {
                ddlChildRoom1Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom1Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom1Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom1Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom2Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom2Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom2Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom2Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom3Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom3Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom3Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom3Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom4Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom4Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom4Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom4Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom5Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom5Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom5Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom5Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom6Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom6Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom6Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom6Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom7Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom7Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom7Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom7Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));

                ddlChildRoom8Child1Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom8Child2Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom8Child3Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                ddlChildRoom8Child4Age.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));


            }
        }
        //ddlChildRoom1Child1Age.Items.Insert(0, new ListItem("?", "-1"));
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        HotelServicesImplService hws = new HotelServicesImplService();

        if (Page.IsValid)
        {

            this.Page.Culture = "en-US";

            Int64 _hotelsearchid = 0;

            string checkindate = inputCheckinDate.Value.Trim();
            string checkoutdate = inputCheckoutDate.Value.Trim();
            string arrivaldate = "";
            string departuredate = "";

            if (inputCheckinDate.Value != "dd/mm/yyyy")
            {
                string[] arrivaldateformatted = checkindate.Split('/');
                arrivaldate = arrivaldateformatted[2] + "-" + arrivaldateformatted[1] + "-" + arrivaldateformatted[0];

                string[] departuredateformatted = checkoutdate.Split('/');
                departuredate = departuredateformatted[2] + "-" + departuredateformatted[1] + "-" + departuredateformatted[0];
            }
            else
            {
                arrivaldate = "1900-01-01 00:00:00.000";
                departuredate = "1900-01-01 00:00:00.000";
            }

            string hotelsearchtext = txtHotelCity.Text.Trim();

            string[] getlocs = hotelsearchtext.Split(',');
            string _city = getlocs[0];
            string _statecode = "";
            string _countryname = "";
            string _countrycode = "";

            if (getlocs.Length == 3)
            {
                _countryname = getlocs[1];
                _countrycode = getlocs[2];
            }
            else if (getlocs.Length == 4)
            {
                _statecode = getlocs[1];
                _countryname = getlocs[2];
                _countrycode = getlocs[3];
            }

            

            if (inputSelectedDestId.Value.Trim() == "")
            {
                string[] location = GetLocationByDestination(txtHotelCity.Text, "city");
                inputSelectedDestId.Value = location[1];                

                getlocs = location[0].Split(',');
                _city = getlocs[0];
                _statecode = "";
                _countryname = "";
                _countrycode = "";

                if (getlocs.Length == 3)
                {
                    _countryname = getlocs[1];
                    _countrycode = getlocs[2];
                }
                else if (getlocs.Length == 4)
                {
                    _statecode = getlocs[1];
                    _countryname = getlocs[2];
                    _countrycode = getlocs[3];
                }
            }
          
            ExpediaHotelInfo expinfo = new ExpediaHotelInfo();

            expinfo.City = _city.Trim(); // "Seattle";
            expinfo.StateProvinceCode = _statecode.Trim(); // "WA";
            expinfo.CountryCode = _countrycode.Trim(); // "US";
            expinfo.ArrivalDate = Convert.ToDateTime(arrivaldate);
            expinfo.DepartureDate = Convert.ToDateTime(departuredate);
            expinfo.NoOfRooms = Convert.ToInt32(ddlRooms.SelectedValue.ToString());
            expinfo.StarRating = 0;
            expinfo.HotelChain = "";
            expinfo.Amenities = "";
            expinfo.Info1 = inputSelectedDestId.Value.Trim();

            ExpediaHotelDB expdb = new ExpediaHotelDB();
            _hotelsearchid = expdb.Insert(expinfo);

            if (_hotelsearchid > 0)
            {
                Session["hotelsearchid"] = _hotelsearchid.ToString();

                int iRoomCount = Convert.ToInt32(ddlRooms.SelectedValue.ToString());                

                for (int i = 0; i < iRoomCount; i++)
                {
                    ExpediaHotelRoomInfo exproominfo = new ExpediaHotelRoomInfo();
                    exproominfo.HotelSearchID = _hotelsearchid;
                    string childage = "";

                    if (i == 0) //Room 1
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom1.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom1.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom1Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom1Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom1Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom1Child4Age.SelectedValue;
                                }
                            }
                        }                        
                    }
                    else if (i == 1)  //Room 2
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom2.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom2.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom2Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom2Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom2Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom2Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 2) //Room 3
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom3.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom3.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom3Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom3Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom3Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom3Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 3) //Room 4
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom4.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom4.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom4Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom4Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom4Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom4Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 4) //Room 5
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom5.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom5.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom5Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom5Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom5Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom5Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 5) //Room 6
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom6.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom6.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom6Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom6Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom6Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom6Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 6) //Room 7
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom7.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom7.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom7Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom7Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom7Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom7Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }
                    else if (i == 7) //Room 8
                    {
                        exproominfo.NoOfAdults = Convert.ToInt32(ddlAdultsRoom8.SelectedValue);
                        exproominfo.NoOfChildren = Convert.ToInt32(ddlChildrenRoom8.SelectedValue);

                        if (exproominfo.NoOfChildren > 0)
                        {
                            for (int c = 0; c < exproominfo.NoOfChildren; c++)
                            {
                                if (c == 0)
                                {
                                    childage = ddlChildRoom8Child1Age.SelectedValue;
                                }
                                if (c == 1)
                                {
                                    childage = childage + ", " + ddlChildRoom8Child2Age.SelectedValue;
                                }
                                if (c == 2)
                                {
                                    childage = childage + ", " + ddlChildRoom8Child3Age.SelectedValue;
                                }
                                if (c == 3)
                                {
                                    childage = childage + ", " + ddlChildRoom8Child4Age.SelectedValue;
                                }
                            }
                        }            
                    }

                    exproominfo.Info1 = childage;

                    ExpediaHotelRoomDB exproomdb = new ExpediaHotelRoomDB();
                    exproomdb.Insert(exproominfo);

                }

                Response.Redirect("results.aspx");
            }
        }

    }
    private string getXMLResult(string requestData)
    {
        WebRequest request = null;
        request = WebRequest.Create(requestData);
        request.Method = "GET";
        WebResponse response = request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        responseFromServer = responseFromServer.Replace("@", "");
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }

}
